using System;
using System.Diagnostics;

namespace Game.Common.Resources
{
    public class GameMovement
    {
        public event EventHandler Changed;

        readonly GameResources _GameResources;
        readonly double _DefaultValue;

        double _Value;

        public GameMovement(
            GameResources gameResources,
            double defaultValue)
        {
            _GameResources = gameResources;
            _DefaultValue = defaultValue;
        }

        public GameResources GameResources
        {
            get { return _GameResources; }
        }

        public double Value
        {
            get { return _Value; }
        }

        public void Decrease(double amount)
        {
            _Value -= amount;
            Debug.Assert(_Value >= 0);
        }

        public void Reset()
        {
            if (_Value != _DefaultValue)
            {
                double fillAmount = _DefaultValue - _Value;

                _GameResources.Fuel.Decrease(fillAmount);
                _Value += fillAmount;

                FireChanged();
            }
        }

        public double Max
        {
            get { return _DefaultValue; }
        }

        void FireChanged()
        {
            if (Changed != null)
            {
                Changed(this, new EventArgs());
            }
        }

        public override string ToString()
        {
            return "{" + _Value + " of " + _DefaultValue + "}";
        }
    }
}